package com.vfa.test;

import java.util.ArrayList;

import com.vfa.vocaburay.R;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class QuestionAdapter extends ArrayAdapter<QuestionE>{

	private ArrayList<QuestionE> items;
	private final LayoutInflater mInflater;
	private final int mLayoutRes;
	private Context context;
	
	private Boolean ishowCheckBox = false;
	TextView  tvquestion, tvnumquestion;
	CheckBox checkquestion;
	private Boolean check = false;
	private int startpoint = 0;
	private QuestionE question;
	
	public QuestionAdapter(Context context,	int textViewResourceId, ArrayList<QuestionE> questionItem) {
		super(context, textViewResourceId, questionItem);
		this.context = context;
		items = questionItem;
		this.mLayoutRes = textViewResourceId;
		this.mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent){
		View v;
		if(convertView == null){
			v=mInflater.inflate(mLayoutRes, parent, false);
		}
		else {
			v=convertView;
		}
		question = items.get(position);
		final int listsize = items.size();
		if(question !=null){
			tvquestion = (TextView) v.findViewById(R.id.textView_QuestionItem);
			tvnumquestion = (TextView)v.findViewById(R.id.textView_numQuestionItem);
			checkquestion = (CheckBox)v.findViewById(R.id.checkBox_question);
			
			if(!ishowCheckBox){
				checkquestion.setVisibility(View.INVISIBLE);
				checkquestion.bringToFront();
			}
			
			if(tvquestion !=null)
			{
				tvquestion.setText(question.getQuestion());
				tvquestion.setPadding(0, 0, 20, 0);
			}
			if(tvnumquestion !=null)
			{
				tvnumquestion.setText(" Q "+(startpoint+position+1)+": ");
			}
			for(int i=0;i<listsize;i++ ){
				
				if((i+1)%2 !=0)
				{
					tvnumquestion.setTextColor(Color.WHITE);
					tvquestion.setTextColor(Color.WHITE);
				}
			}
			
			checkquestion.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					//int pos= buttonView.get
					if(isChecked)
					{
						items.get(position).setChecked(true);
					}
					else {
						items.get(position).setChecked(false);
					}
					
				}
			});
			
			if (items.get(position).isChecked()) {
				checkquestion.setChecked(true);
				System.out.println(position+" check ");
			} else {
				checkquestion.setChecked(false);
			}
		}
		return v;
	}
	
	
	private void setQuestionChecked(){
		for(int i=0;i<items.size();i++)
		{
			question = items.get(i);
			if (question.isChecked()) {
				checkquestion.setChecked(true);
				System.out.println(" check "+ i);
			}else {
				checkquestion.setChecked(false);
			}
		}
	}
	public void setNoquestion(int startpoint){
		this.startpoint = startpoint;
	}
	
	public void showCheckBox( Boolean isshow){
			this.ishowCheckBox = isshow;
	}
}
